// select_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/select.h>

#define PORT 8888
#define MAX_CLIENTS 5
#define BUFFER_SIZE 1024

int main() {
    int server_fd, client_sockets[MAX_CLIENTS];
    struct sockaddr_in address;
    int addrlen = sizeof(address);
    fd_set readfds;
    int max_sd;
    char buffer[BUFFER_SIZE] = {0};

    // 创建 socket 文件描述符
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    // 绑定 socket 到端口
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    // 监听传入连接
    if (listen(server_fd, MAX_CLIENTS) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    // 初始化客户端 socket 数组
    for (int i = 0; i < MAX_CLIENTS; i++) {
        client_sockets[i] = 0;
    }

    printf("Server listening on port %d...\n", PORT);

    while (1) {
        // 清空文件描述符集
        FD_ZERO(&readfds);

        // 添加服务器 socket 到集合
        FD_SET(server_fd, &readfds);
        max_sd = server_fd;

        // 添加客户端 sockets 到集合
        for (int i = 0; i < MAX_CLIENTS; i++) {
            if (client_sockets[i] > 0) {
                FD_SET(client_sockets[i], &readfds);
            }

            if (client_sockets[i] > max_sd) {
                max_sd = client_sockets[i];
            }
        }

        // 等待活动的 socket
        int activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);

        if (activity < 0) {
            perror("select error");
            exit(EXIT_FAILURE);
        }

        // 如果有新的连接请求
        if (FD_ISSET(server_fd, &readfds)) {
            if ((client_sockets[0] = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
                perror("accept");
                exit(EXIT_FAILURE);
            }

            printf("New connection accepted\n");

            // 将新连接移动到客户端 sockets 数组
            for (int i = 0; i < MAX_CLIENTS; i++) {
                if (client_sockets[i] == 0) {
                    client_sockets[i] = client_sockets[0];
                    client_sockets[0] = 0;
                    break;
                }
            }
        }

        // 检查客户端 sockets 的活动
        for (int i = 0; i < MAX_CLIENTS; i++) {
            if (client_sockets[i] > 0 && FD_ISSET(client_sockets[i], &readfds)) {
                int valread = read(client_sockets[i], buffer, BUFFER_SIZE);
                if (valread <= 0) {
                    // 客户端断开连接
                    printf("Client disconnected\n");
                    close(client_sockets[i]);
                    client_sockets[i] = 0;
                } else {
                    printf("Received from client: %s\n", buffer);
                    send(client_sockets[i], buffer, strlen(buffer), 0);
                    memset(buffer, 0, sizeof(buffer));
                }
            }
        }
    }

    return 0;
}
